package com.nlscan.qdkit.ui.publico.task;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

// TODO: 2019/6/8 区分sdk处理
public class ThreadPool {
    private static ThreadPool instance = new ThreadPool();
    private ExecutorService defaultExecutor = Executors.newCachedThreadPool();


    /**
     * 用于处理回放各个窗口下载图片操作
     */
    @Deprecated
    private ExecutorService downloadExecutor0 = createDownloadExecutor(2);
    private ExecutorService downloadExecutor1 = createDownloadExecutor(2);
    private ExecutorService downloadExecutor2 = createDownloadExecutor(2);
    private ExecutorService downloadExecutor3 = createDownloadExecutor(2);
    private ExecutorService downloadSingleExecutor = createDownloadExecutor(4);

    /**
     * 用于预览
     */
    private ExecutorService previewExecutor = createDownloadExecutor(16);

    /**
     * 用于打印.so日志
     */
    private ExecutorService sdkLogExecutor = Executors.newSingleThreadExecutor();


    private ThreadPool() {
    }

    public static ThreadPool getInstance() {
        return instance;
    }

    /**
     * 获取默认的CacheExecutor
     */
    public ExecutorService getDefaultExecutor() {
        return defaultExecutor;
    }

    public ExecutorService getDownloadExecutor0() {
        return downloadExecutor0;
    }

    public ExecutorService getDownloadExecutor1() {
        return downloadExecutor1;
    }

    public ExecutorService getDownloadExecutor2() {
        return downloadExecutor2;
    }

    public ExecutorService getDownloadExecutor3() {
        return downloadExecutor3;
    }

    public ExecutorService getDownloadSingleExecutor() {
        return downloadSingleExecutor;
    }

    public ExecutorService getPreviewExecutor() {
        return previewExecutor;
    }

    public ExecutorService getHs_SdkLogExecutor() {
        return sdkLogExecutor;
    }

    /**
     * 创建下载图片线程池
     *
     * @param nThreads 并行下载线程数
     * @return 下载图片的线程池
     */
    public static ExecutorService createDownloadExecutor(int nThreads) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(nThreads, nThreads,
                60L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>());
        executor.allowCoreThreadTimeOut(true);
        return executor;
    }

}
